home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / StringChooser.java < prev    next >
Text File  |  1998-06-30  |  4KB  |  120 lines

  1. /*
  2.  * @(#)StringChooser.java    1.9 01/28/98
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * @author James Gosling
  20.  */
  21.  
  22. package com.sun.java.swing;
  23.  
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import java.io.*;
  27. import com.sun.java.swing.event.*;
  28.  
  29. public class StringChooser {
  30.     /** Convenience method to prompt for a String.
  31.     @param parent the parent component for the dialog box.
  32.         parent may be null if a default parent has
  33.         been established with StandardDialog
  34.     @param description a description string that will be shown
  35.         to the user to indicate what is being requested
  36.     @param initial the initial value for the string
  37.     @param columns the default width (in 'm's) of the TextField
  38.     @param target the ChangeListener that will be informed if the
  39.             OK or Apply buttons are hit.
  40.     @return If target is null,
  41.         the dialog box will be modal and the method
  42.         will return the final result, or null if canceled.
  43.         Otherwise, the
  44.         dialog box is non-modal, the method returns null
  45.         immediatly, and the listener is informed when
  46.         appropriate.  The source of the change event will
  47.         be a JTextField.
  48.     @see StandardDialog
  49.      */
  50.     public static String ask(Component parent, String description,
  51.                           String initial, int columns,
  52.                       ChangeListener target) {
  53.     if (columns <= 0)
  54.         columns = 10;
  55.     JTextField t = new JTextField(initial, columns);
  56.     StandardDialog d = new StandardDialog (parent, t, target==null);
  57.     d.setDescription(description);
  58.     d.setStyle(StandardDialog.QuestionStyle);
  59.     if (target != null)
  60.         d.addChangeListener(target);
  61.     t.selectAll();
  62.     d.start();
  63.     if (target != null) return null;
  64.     String r = t.getText();
  65.     d.dispose();
  66.     return d.isCanceled() ? null : r;
  67.     }
  68.     /** Convenience method to prompt for a String from a set of Strings.
  69.     @param parent the parent component for the dialog box.
  70.         parent may be null if a default parent has
  71.         been established with StandardDialog
  72.     @param description a description string that will be shown
  73.         to the user to indicate what is being requested
  74.     @param set the set of strings
  75.     @param initial the default choice from the set
  76.     @param target the ChangeListener that will be informed if the
  77.             OK or Apply buttons are hit.
  78.     @return If target is null,
  79.         the dialog box will be modal and the method
  80.         will return the index of string from the set, or -1 if canceled.
  81.         Otherwise, the
  82.         dialog box is non-modal, the method returns 0
  83.         immediatly, and the listener is informed when
  84.         appropriate.  The source of the change event will
  85.         be a JComboBox.
  86.     @see StandardDialog
  87.      */
  88.     public static int ask(Component parent, String description,
  89.                           final String set[], final int initial,
  90.                       ChangeListener target) {
  91.     if (set==null || set.length==0) return -1;
  92.     JComboBox t = new JComboBox(new ComboBoxModel() {
  93.         int inx = initial;
  94.         public Object getCurrentValue() { return set[inx]; }
  95.         public void setCurrentValue(Object o) {
  96.             for (int i = set.length; --i>=0; )
  97.             if (o.equals(set[i])) {
  98.                 inx = i;
  99.                 return;
  100.             }
  101.         }
  102.         public int getSize() { return set.length;}
  103.         public Object getElementAt(int i) { return set[i]; }
  104.         public void addListDataListener(ListDataListener l) {}
  105.         public void removeListDataListener(ListDataListener l) {}
  106.     });
  107.     t.setCurrentValueIndex(initial);
  108.     StandardDialog d = new StandardDialog (parent, t, target==null);
  109.     d.setDescription(description);
  110.     d.setStyle(StandardDialog.QuestionStyle);
  111.     if (target != null)
  112.         d.addChangeListener(target);
  113.     d.start();
  114.     if (target != null) return 0;
  115.     int r = t.getCurrentValueIndex();
  116.     d.dispose();
  117.     return d.isCanceled() ? -1 : r;
  118.     }
  119. }
  120.